from configparser import ConfigParser
from loguru import logger
from utils.handle_path import PollyPath
from time import strftime


class MyLog:
    __instance = None  # 单例实现
    __call_flag = True  # 控制init调用

    def __new__(cls, *args, **kwargs):
        if not cls.__instance:
            cls.__instance = super().__new__(cls)
        return cls.__instance

    def get_log(self):
        if self.__call_flag:
            curdate = strftime("%Y%m%d_%H%M%S")
            cfg = ConfigParser()  # 标准ConfigParser 读取ini文件
            cfg.read(PollyPath.configs_path / 'loguru.ini', encoding="utf-8")
            logger.remove(handler_id=None)  # 关闭console输出
            logger.add(
                PollyPath.logs_path / f"AutoPolly_{curdate}.log",  # 日志存放位置
                retention=cfg.get('log', 'retention'),  # 清理
                format=cfg.get('log', 'format'),  # 日志输出格式
                # compression=cfg.get('log', 'compression'),  # 日志压缩格式
                level=cfg.get('log', 'level')  # 日志级别
            )
            self.__call_flag = False  # 如果用过就设置为False
        return logger


log = MyLog().get_log()

if __name__ == '__main__':
    curdate = 1
    print(PollyPath.logs_path / f"AutoPolly_{curdate}.log")
    test_flag = 1
    log.info("测试")
    if test_flag == 1:
        log.error('李四')
        from time import sleep
        sleep(5)
        log.error('张三')

